<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Hesat	Zendeli
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |21.3.2010 Erstellung | Hesat Zendeli		   |
 |_____________________________________________|

******************************************************************************

P3(c) */
class Language extends Base
{
	public function Language()
	{
		$this->title = $GLOBALS['lang']->GetLanguage("Language", "title");
		$this->toolbar = true;
		$this->moduleID = 120;
		$this->permission = GetPermission($this->moduleID);
		$this->toolbarClass = "icon-48-language";
		
		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
	}
	
	private function CheckInput()
	{
		include("./_tables.php");
		
		if($_POST['langCode'] == "" || $_POST['langDesc'] == "")
		{
			$this->errorText = $GLOBALS['lang']->GetLanguage("Panel", "missedMandatoryField");
			return false;
		}
		
		$query = "SELECT * FROM ".$tbllang." WHERE langCode='".$_POST['langCode']."'";
		if($GLOBALS['db']->Query($query)->num_rows)
		{		
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
				
			if($_POST['id'] != $res->id)
			{
				$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
				$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Language", "langCode"), $string);
				$string = str_replace("{1}", $_POST['langCode'], $string);
				
				$this->errorText = $string;
				return false;
			}
		}
		
		$query = "SELECT * FROM ".$tbllang." WHERE langDesc='".$_POST['langDesc']."'";

		if($GLOBALS['db']->Query($query)->num_rows)
		{
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
				
			if($_POST['id'] != $res->id)
			{
				$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
				$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Language", "langDesc"), $string);
				$string = str_replace("{1}", $_POST['langDesc'], $string);
				
				$this->errorText = $string;
				return false;
			}
		}
		
		return true;
	}
	public function CheckFastEditInput()
	{
		include("./_tables.php");
		
		if($_GET['field'] == "langCode")
		{
			$query = "SELECT * FROM ".$tbllang." WHERE langCode='".$_GET['value']."'";
			if($GLOBALS['db']->Query($query)->num_rows)
			{
				$res = $GLOBALS['db']->Fetch($GLOBALS->Query($query));
					
				if($_GET['id'] != $res->id)
				{
					$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
					$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Language", "langCode"), $string);
					$string = str_replace("{1}", $_GET['value'], $string);
					
					echo "//<script>
					alert('".$string."');
					Redirect('index.php?s=language');
					//</script>";
					return false;
				}
			}
		}
		else if($_GET['field'] == "langDesc")
		{
			$query = "SELECT * FROM ".$tbllang." WHERE langDesc='".$_GET['value']."'";
			if($GLOBALS['db']->Query($query)->num_rows)
			{
				$res = $GLOBALS['db']->Fetch($GLOBALS->Query($query));
					
				if($_GET['id'] != $res->id)
				{
					$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
					$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Language", "langDesc"), $string);
					$string = str_replace("{1}", $_GET['value'], $string);
					
					echo "//<script>
					alert('".$string."');
					Redirect('index.php?s=language');
					//</script>";
					return false;
				}
			}
		}
		
		return true;
	}
	
	public function Add()
	{		
		include("./_tables.php");
		
		$smarty = GetSmarty();
		
		if($this->error)
		{
			$smarty->assign("error",  $this->errorText);
		}


		$smarty->assign("langCode",  $_POST['langCode']);
		$smarty->assign("langDesc",  $_POST['langDesc']);
		
		$smarty->assign("type", "save");
		$smarty->assign('hiddenFields', array( 
							array('name' => 'action','value' => 'save'),
                            array('name' => 'option','value' => 'language')));
	
		$smarty->display('base/language.tpl');
	}
	public function Edit()
	{		
		include("./_tables.php");
		
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tbllang." WHERE id=".$_POST['id'].""));

		$smarty = GetSmarty();
		
		if(isset($_POST['username']))
		{
			if($this->error)
			{
				$smarty->assign("error",  $this->errorText);
			}
			$res->langCode = $_POST['langCode'];
			$res->langDesc = $_POST['langDesc'];
		}
		
		

		$smarty->assign("langCode",  $res->langCode);
		$smarty->assign("langDesc", $res->langDesc);
		
		$smarty->assign("type", "update");
		$smarty->assign('hiddenFields', array( 
							array('name' => 'action','value' => 'update'),
                            array('name' => 'option','value' => 'language'),
                            array('name' => 'id','value' => $_POST["id"])));
		
		
		$smarty->display('base/language.tpl');
	}
	
	public function AnalysPost()
	{
		include("./_tables.php");
		$this->filter = $_POST['search'];
					
		if($_POST['action'] == "add")
		{
			$this->Add();
		}
		else if($_POST['action'] == "edit")
		{
			$this->Edit();
		}
		else if($_POST['action'] == "search")
		{
			$this->Paint();
		}
		else if($_POST['action'] == "paint")
		{
			$this->filter = "";
			$this->Paint();
		}
		else if($_POST['action'] == "save")
		{
			if($this->CheckInput())
			{
				$GLOBALS['db']->SaveObject($tbllang);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Add();
			}
		}
		else if($_POST['action'] == "update")
		{
			if($this->CheckInput())
			{
				$GLOBALS['db']->UpdateObject($tbllang);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Edit();
			}
		}
		else if($_POST['action'] == "delete")
		{
			$GLOBALS['db']->Delete($tbllang);
			$this->Paint();
		}
		else
		{
			$this->Paint();
		}
	}

	public function Paint()
	{
		include("./_tables.php");
		
		echo 
		'<div id="cpanel">';
				
		$dgv = new DataGridView($this);
		
		$dgv->OpenHeader();
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Language", "langCode"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Language", "langDesc"), "17%", true);
		$dgv->AddHeader("", "90%", true);
		$dgv->CloseHeader();
		
		$res = $GLOBALS['db']->Query("SELECT * FROM ".$tbllang);
		
		if($this->permission->editPermission)
			$title = "fastEdit";
		else
			$title = "";
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			$dgv->AddRow($line->id);
			$dgv->AddData($line->langCode, $title, "", "langCode");
			$dgv->AddData($line->langDesc, $title, "", "langDesc");
			$dgv->AddData("", "", "", "");
			
			$dgv->CloseRow();
		}
		
		$dgv->CloseDataGrid();
		
		echo '</div>';
	}
	public function Toolbar()
	{	
		if(!$this->allowed)
		{
			return;
		}
		
			
		PaintDefaultToolbar($this);
	}
	
	public function AddCSS()
	{
			
	}
	public function AddJS()
	{
		
	}
}

?>